package tiltmaze.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import tiltmaze.Cell;
import tiltmaze.Maze;

/**
 * Class used for displaying a non-interactive maze
 * @author Juraj Niznan
 */
public class MazeDisplay extends Component {

    public static int SQUARE_SIZE = 10;
    public static int BORDER = 20;
    private MazeRectangle[][] verticalRectangles;
    private MazeRectangle[][] horizontalRectangles;
    private MazeRectangle[][] checkpoints;
    private int width;
    private int height;
    private Ball ball;

    /**
     * Constructor. Creates graphical representation according to the maze provided.
     * @param maze
     */
    public MazeDisplay(Maze maze) {
        width = maze.getNumOfCols();
        height = maze.getNumOfRows();
        Cell[][] cells = maze.getCells();
        verticalRectangles = new MazeRectangle[height][width + 1];
        horizontalRectangles = new MazeRectangle[height + 1][width];
        checkpoints = new MazeRectangle[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j <= width; j++) {
                verticalRectangles[i][j] = new MazeRectangle(i, j, MazeRectangle.VERTICAL_WALL);
                verticalRectangles[i][j].visible = (j < width) ? cells[i][j].getWalls()[3] : cells[i][j - 1].getWalls()[1];
            }
        }
        for (int i = 0; i <= height; i++) {
            for (int j = 0; j < width; j++) {
                horizontalRectangles[i][j] = new MazeRectangle(i, j, MazeRectangle.HORIZONTAL_WALL);
                horizontalRectangles[i][j].visible = (i < height) ? cells[i][j].getWalls()[0] : cells[i - 1][j].getWalls()[2];
            }
        }
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                checkpoints[i][j] = new MazeRectangle(i, j, MazeRectangle.CHECKPOINT);
                checkpoints[i][j].visible = cells[i][j].getCheckpoint();
                if (cells[i][j].getStart()) {
                    ball = new Ball(i, j, width, height);
                }
            }
        }
        this.setBackground(Color.white);
        this.setPreferredSize(new Dimension(BORDER * 2 + SQUARE_SIZE * (5 * width + 1), BORDER * 2 + SQUARE_SIZE * (5 * height + 1)));
    }

    @Override
    public void paint(Graphics graphics) {
        Graphics2D g = (Graphics2D) graphics;
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        //draw black walls:
        g.setColor(Color.BLACK);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j <= width; j++) {
                if (!verticalRectangles[i][j].hovered && verticalRectangles[i][j].visible) {
                    g.fill(verticalRectangles[i][j]);
                }

            }
        }
        for (int i = 0; i <= height; i++) {
            for (int j = 0; j < width; j++) {
                if (!horizontalRectangles[i][j].hovered && horizontalRectangles[i][j].visible) {
                    g.fill(horizontalRectangles[i][j]);
                }
            }
        }
        //draw checkpoint:
        g.setColor(Color.BLUE);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (checkpoints[i][j].visible) {
                    g.fill(checkpoints[i][j]);
                }
            }
        }
        //draw ball:
        g.setColor(Color.RED);
        g.fill(ball);

    }
}
